<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * programsav CRUD model class 
 */
Class programsav_model extends CI_Model {

    public $programsav_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->programsav_table = $this->db->dbprefix('programsav');
    }

    /**
     * 
     * 
     * 
     * Update programsav by id.
     * 
     * 
     * @return true on success
     */
    function update_programsav($programsav_data = array()) {
        // if programsav does not exists throw programsav_not_exists_exception
        if ($this->get_programsav($programsav_data['programsav_id']) == FALSE)
            throw new not_exists_exception("programsav");

        // do selection and update data
        $this->db->where('programsav_id', $programsav_data['programsav_id']);
        $this->db->update($this->programsav_table, $programsav_data);
    }

    /**
     * 
     * 
     * 
     * Delete programsav
     * 
     * 
     * 
     * @param type $id
     */
    function delete($programsav_id) {
        // delete programsav data from programsav_table
        $this->db->where('programsav_id', $programsav_id);
        $this->db->delete($this->programsav_table);
    }

    /**
     * 
     * Add new programsav
     * 
     * @param type $programsav_data
     */
    function add_programsav($programsav_data = array()) {
        // insert programsav_data
        $this->db->insert($this->programsav_table, $programsav_data);
    }

    /**
     * 
     * returns the programsavs (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned programsavs
     * @param type $offset from this number.
     * @return type 
     */
    function get_programsavok($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (isset($options['search_like'])) {
            $this->db->or_like('programsav_nev', $options['search_like']);
            $this->db->or_like('programsav_rovid_leiras', $options['search_like']);
            // todo programsav!! itt kell csinálni valami átalakítást, kitalálni!!
            $this->db->or_like('programsav_szin', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->programsav_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of programsavs
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->programsav_table);
    }

    /**
     * Gives back programsav by id 
     * 
     * @param type $id
     * @return array (programsav_id, programsavname, programsav)
     */
    function get_programsav($id) {
        // constructing active record query
        $query = $this->db->get_where($this->programsav_table, array('programsav_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

}